<?php
class Commsubq extends Base_Controller
{
    public function __construct()
    {
        /********待编辑********/
        $this->_page_size = 10;
        $this->_form_type = 'comm_subq';
        $this->_list_where = array();
        $this->_action_list = array('DEL');
        $this->_controller = 'admin/commsubq/';
        parent::__construct();
    }

    public function showList($qid = 0,$page = 1)
    {
        $this->_external_id = $qid;
        $this->_list_where = array('qid'=>$qid);

        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;
        $pdata = $this->getPagination($this->_controller.'showList/'.$this->_external_id.'/');

        $res = $this->getList($offset,$limit);
        //filter list data
        foreach ($res['list'] as &$row){
            $sub = $this->adminform_model->getRowData('comm_subject','name,icon',array('id'=>$row['subid']));
            $row['subid'] = $sub['name'];
            $sub['icon'] = '<img src="'.$this->_resource_url.$sub['icon'].'" style="height:30px;">';
            array_splice($row,0,0,$sub['icon']);
        }
        array_splice($res['headers'],0,0,'图标');

        $createbtn = $this->load->view('admin/form/createbtn',
            array('controller'=>site_url($this->_controller),'param'=>$this->_external_id),true);
        $backbtn = $this->load->view('admin/form/backbtn',array('controller'=>site_url('admin/commquestion/')),true);
        $data = array('headers'=>$res['headers'],'list'=>$res['list'],'pagination'=>$pdata['pagination'],'createbtn'=>$createbtn,'backbtn'=>$backbtn);

        $content = $this->load->view('admin/form/list',$data,true);
        echo $this->getAdminView($content);

    }

    public function create($qid = 0)
    {
        $this->_external_id = $qid;
        $post = $this->input->post();

        if(empty($post)){
            $form = $this->createBuildForm();
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),
                'param'=>$this->_external_id,'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /********待编辑********/
            //filter post data
            $post['qid'] = $qid;
            $post['created'] = time();

            $this->_create_data = $post;
            parent::create();
        }
    }

    public function aftercreate($id)
    {
        $subq = $this->adminform_model->getRowData('comm_subq_relations','subid',array('id'=>$id));
        if (!empty($subq['subid'])){
            $this->load->model('redis_model');
            $this->redis_model->hincrby('subject:'.$subq['subid'],'questions',1);
        }
    }

    public function delete($id = 0,$qid = 0)
    {
        $this->_external_id = $qid;
        parent::delete($id);
    }

    public function afterdelete($pkey,$pkeyval)
    {
        $subq = $this->adminform_model->getRowData('comm_subq_relations','subid',array($pkey=>$pkeyval));
        if (!empty($subq['subid'])){
            $this->load->model('redis_model');
            $this->redis_model->hincrby('subject:'.$subq['subid'],'questions',-1);
        }
    }

}